//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls
{
	public class ChangeViewPopupMenu : ClientPopulatedPopupMenu
	{
	    CSContext csContext;

		#region Event Handlers

		protected override void OnInit(EventArgs e)
		{
			csContext = CSContext.Current;
			this.ResourceName = "ChangeViewPopupMenu_Text";

			if (csContext.User.IsAnonymous)
				this.Visible = false;
		}

		protected override void OnPreRender(EventArgs e)
		{
			if (this.ShowBackgroundImageIcon)
				this.Style["background-image"] = "url(" + Globals.GetSkinPath() + "/images/common/view.gif)";

			base.OnPreRender(e);
		}

		#endregion

		#region Render Methods

		protected override ArrayList ClientMenuItemFormats
		{
			get
			{
				ArrayList formats = new ArrayList();

				if (PostID != -1)
				{
					ClientMenuItemFormat format;

				    Thread thread = Threads.GetThread(Posts.GetPost(PostID, csContext.User.UserID).ThreadID);

					format = new ClientMenuItemFormat();
					format.ClientSideDisplayFunction = "new Function('parms', 'return true;')";
					format.Text = ResourceManager.GetString("ChangeViewPopupMenu_Flat");
					format.NavigateUrl = SiteUrls.Instance().Post(thread.PostID, PostViewType.Flat);
					format.Target = "_top";
					formats.Add(format);

					format = new ClientMenuItemFormat();
					format.ClientSideDisplayFunction = "new Function('parms', 'return true;')";
					format.Text = ResourceManager.GetString("ChangeViewPopupMenu_Threaded");
					format.NavigateUrl = SiteUrls.Instance().Post(thread.PostID, PostViewType.Threaded);
					format.Target = "_top";
					formats.Add(format);
				}

				return formats;
			}
		}

		#endregion

		#region Properties

		public virtual bool ShowBackgroundImageIcon
		{
			get
			{
				object state = ViewState["ShowBackgroundImageIcon"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["ShowBackgroundImageIcon"] = value;
			}
		}

		public virtual int PostID
		{
			get
			{
				object state = ViewState["PostID"];
				if (state == null)
					return csContext.PostID;
				else
					return (int) state;
			}
			set
			{
				ViewState["PostID"] = value;
			}
		}

		#endregion
	}
}
